package il.ac.tau.arielgue.outxing.contexts;

import il.ac.tau.arielgue.outxing.agents.OutXingOrganism;
import il.ac.tau.arielgue.outxing.utils.Params;

import java.util.Collection;
import java.util.HashSet;

import proevolution.agents.Organism;

/**
 * 
 * @author arielgue
 * 
 */
public class PoolSubContext extends AbstractOutXingSubContext {

	private static final long serialVersionUID = 2680297912510014530L;
	
	private double fitnessThreshold = Double.NaN;

	public PoolSubContext() {
		super();
	}

	public PoolSubContext(Object name, Object typeID) {
		super(name, typeID);
	}

	public PoolSubContext(Object name) {
		super(name);
	}
	
	/**
	 * called by master context. chooses organisms and transfers them to a
	 * random deme.
	 * 
	 * @deprecated 7.3.10 - Ariel's decided newborns abandon always and parents
	 *             die, so there is no abandoning.
	 */
	@Deprecated
	public Collection<Organism> abandon() {
		Collection<Organism> abandoners = new HashSet<Organism>();
		double threshold = getFitnessThreshold();
		for (Organism o : getOrganisms()) {
			if (o.getFitness() > threshold) {
				((OutXingOrganism) o).modifyFitness(1 - Params
						.getOutXingFitnessCost());
				remove(o);
				abandoners.add(o);
			}
		}
		return abandoners;
	}

	public void setFitnessThreshold(double fitnessThreshold) {
		this.fitnessThreshold = fitnessThreshold;
	}

	public double getFitnessThreshold() {
		return fitnessThreshold;
	}
	public int getNumOfPoolers() {//debug method
		int n=0;
		for (Organism o : getOrganisms()) {
			if (o instanceof OutXingOrganism) {
				n+=((OutXingOrganism)o).getMovedToPool();
			}
		}
		return n;
	}

}
